<?php
/** .-------------------------------------------------------------------
* |  Software: [HDCMS framework]
* |      Site: www.hdcms.com
* |-------------------------------------------------------------------
* |    Author: 向军 <2300071698@qq.com>
* |    WeChat: aihoudun
* | Copyright (c) 2012-2019, www.houdunwang.com. All Rights Reserved.
* '-------------------------------------------------------------------*/

namespace app\admin\controller;

class Type extends Common {
    private $db;
    public function __construct()
    {
        parent::__construct();
        $this->db=new \system\model\Type();
    }

    //动作
    public function index(){
    //此处书写代码...
        $data=Db::table('type')->paginate(7);
//        p($data);die;
        $page=$data->links();
        View::with("data",$data)->with("page",$page);
        return view();
    }
    //添加
    public function add(){
        if(IS_POST){
            if($this->db->store()){
                message("添加成功",u('type.index'),"success");
            }
            message($this->db->getError(),"" ,"error");
        }
        return view();
    }
    //编辑
    public function edit(){
        //post提交
        if(IS_POST){
            //模型返回数据
            if($this->db->edit()){
                //成功
                message('编辑成功','','success');
            }
            //失败
            message($this->db->getError(),'','error');
        }
        $tid =Q('get.tid');
        $oldData = Db::table('type')->where('tid',$tid)->first();
        View::with('oldData',$oldData);
        return view();
    }
    //删除
    public function del(){
        //get参数
        $tid =(int)Q('t');
        $data = Db::table('type_attr')->where("tid",$tid)->first();
        if(!$data){
            //执行删除父级那一条数据
            $this->db->where('tid',$tid)->delete();
            //提示跳转
            message('删除成功','','success');
        }else{
            message("该数据下存在子集，禁止删除","","error");
        }

    }
}
